<template>
  <div class="tab-container">
    <button class="btn" @click="goBack">Back</button>
    <el-tabs v-model="activeName" style="margin-top:15px;" type="border-card">
      <el-tab-pane v-for="item in tabMapOptions" :key="item.key" :label="item.label" :name="item.key" >
        <keep-alive>
          <tab-pane 
            v-if="activeName==item.key" 
            :type="item.key" 
            :list="taskArray"
            @create="showCreatedTimes" 
          />
        </keep-alive>
      </el-tab-pane>
    </el-tabs>
    <input v-model="rid" type="text" placeholder="请输入要查询的rid" class="input-item">
    <button class="btn" @click="Download">Back</button>
  </div>
</template>

<script>
import Download from '../pdf/download.vue';
import TabPane from './components/TabPane'

import table_data from './components/table_data.vue'
import axios from 'axios'
axios.defaults.withCredentials = true
export default {
  name: 'Tab',
  components: { TabPane, table_data },
  data() {
    return {
      tabMapOptions: [
        { label: 'China', key: 'CN' },
        { label: 'USA', key: 'US' },
        { label: 'Japan', key: 'JP' },
        { label: 'Eurozone', key: 'EU' }
      ],
      activeName: 'US',
      createdTimes: 0,
      taskArray: [{
          id: 1,
          title: '任务1',
          author: 'filepath',
          pageviews: 1,
          status: 1
        }],
      rid: ''
    }
  },
  watch: {
    activeName(val) {
      this.$router.push(`${this.$route.path}?tab=${val}`)
    }
  },
  created() {
    const tab = this.$route.query.tab
    if (tab) {
      this.activeName = tab
    }
  },
  methods: {
    showCreatedTimes() {
      this.createdTimes = this.createdTimes + 1
    },
    goBack() {
      var myHeaders = new Headers()
      myHeaders.append('User-Agent', 'Apifox/1.0.0 (https://apifox.com)')
      myHeaders.append('Authorization', 'Bearer ' + sessionStorage.getItem('token'))
      var requestOptions = {
        method: 'GET',
        headers: myHeaders,
        redirect: 'follow'
      }

      fetch('http://1.92.204.101:5000/report/check_report', requestOptions)
        .then(response => response.text())
        .then(result => {
          var data = JSON.parse(result)
          for (var i = 0; i < data.length; i++) {
            var da = data[i]
            this.taskArray.push(da)
            console.log(da)
          }
          console.log(this.taskArray)
        })
        .catch(error => console.log('error', error))
    },
    Download(){
      var myHeaders = new Headers();
      myHeaders.append("User-Agent", "Apifox/1.0.0 (https://apifox.com)");
      myHeaders.append("Content-Type", "application/json");
      myHeaders.append('Authorization', 'Bearer ' + sessionStorage.getItem('token'))

      var requestOptions = {
        method: 'GET',
        headers: myHeaders,
        redirect: 'follow'
      };

fetch("http://1.92.204.101:5000/report/download_report/" + this.rid, requestOptions)
.then(response => response.blob())
        .then(blob => {
          const url = window.URL.createObjectURL(blob);
          const a = document.createElement('a');
          a.style.display = 'none';
          a.href = url;
          a.download = 'example_file.txt';
          document.body.appendChild(a);
          a.click();
          window.URL.revokeObjectURL(url);
        })
        .catch(error => {
          console.error('下载文件失败:', error);
        });

    }
  }
}
</script>

<style scoped>
  .tab-container {
    margin: 30px;
  }
</style>
